<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="发现的问题" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-row :gutter="20">
            <el-col :span="12">
                <el-form-item label="发现问题时间" prop="description_date">
                    <el-date-picker style="width: 100%" v-model="ruleForm.description_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" />
                </el-form-item>
                <el-form-item label="隐患照片" prop="description">
                    <el-image style="width: 200px; height: 200px" @click="onUploadFile('description')" :src="ruleForm.description" />
                </el-form-item>
            </el-col>

            <el-col :span="12">
                <el-form-item label="整改完成时间" >
                    <el-date-picker style="width: 100%" v-model="ruleForm.rectification_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" />
                </el-form-item>
                <el-form-item label="整改后照片" >
                    <el-image style="width: 200px; height: 200px" @click="onUploadFile('rectification')" :src="ruleForm.rectification" />
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>

    <el-dialog v-model="showUpload" title="文件">
        <component :is="upload" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>

</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../util/request';
import upload from '@/components/upload.vue'
const loading = ref<boolean>(false)
const showUpload = ref<boolean>(false)
let uploadData:any = ref({prefix:'hazard/',type:''})

interface RuleForm {
    id: number
    type:string
    title:string
    description_date:string
    description:string
    rectification_date: string
    rectification:string
}

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    type:'',
    title:'',
    description_date: '',
    description: '',
    rectification_date: '',
    rectification: '',
})

const onGetData = () => {
    http.get('/hazard/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
    })
}


const props = defineProps<{
    data:any,
    type:string
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.type = props.type
    onGetData()
},{ immediate: true })


const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入发现问题', trigger: 'blur' },
    ],
    description_date: [
        { required: true, message: '请输入发现问题时间', trigger: 'blur' },
    ],
    description: [
        { required: true, message: '请上传隐患照片', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/hazard',ruleForm.value)
            }else{
                request = http.put('/hazard',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onUploadFile = (type:string) => {
    uploadData.value.type = type
    if(type == 'description'){
        uploadData.value.image = ruleForm.value.description
    }

    if(type == 'rectification'){
        uploadData.value.image = ruleForm.value.rectification
    }
    
    showUpload.value = true
}

const saveUpload = (data:string) => {
    if(uploadData.value.type == 'description'){
        ruleForm.value.description = data
    }

    if(uploadData.value.type == 'rectification'){
        ruleForm.value.rectification = data
    }

    showUpload.value = false
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}
</style>
